// pages/playList/playList.js
import Pubsub from 'pubsub-js'
import {getUserRecord} from '~/service/personal'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playList:[],
    tapIndex:0, //点击跳转项的索引值,
    profile:{}
  },
  // 点击歌曲跳转到歌曲详情页面(播放)
  toSongDetail(e){
    let {songid,index} = e.currentTarget.dataset;
    this.setData({
      tapIndex:index
    })
    wx.navigateTo({
      url: `/pages/songDetail/songDetail?songid=${songid}`,
    })
  },
  // 获取最近播放列表
  async getUserRecord(){
    // let {allData} = await getUserRecord({uid:this.data.profile.userId,type:0})
    let {data:{list}} = await getUserRecord({cookie:JSON.parse(wx.getStorageSync('cookie'))})
    let last20 = list.slice(0,20)
    // console.log(data)
    this.setData({
      playList:last20
    })
    console.log(this.data.playList)
  }
  ,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      profile:JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.getUserRecord()

    Pubsub.subscribe('switchType',(msg,type)=>{
      let index = this.data.tapIndex;
      if(type=='next'){
        index++; //下一首歌曲项的索引值
        (index > this.data.dailySongs.length-1) && (index = 0)
      }else if (type=='pre'){
        index--
        (index < 0)  && (index = this.data.dailySongs.length-1)
      }
      this.setData({
        tapIndex:index
      })
      let  musicId = this.data.dailySongs[index].id
      Pubsub.publish('musicId',musicId)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})